package com.lancoo.znbkxx.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lancoo.znbkxx.R;
import com.lancoo.znbkxx.adapter.WrongItemAdapter;
import com.lancoo.znbkxx.beans.WrongFragmentBean;
import com.lancoo.znbkxx.beans.WrongItemListBean;
import com.lancoo.znbkxx.uis.PhoneMainActivity;

import java.util.ArrayList;
import java.util.List;

import znbkCNtxl.CNtxlActivity;
import znbkCNtxl.GetNtxlJson.PostCNtxlJsonBody;

/**
 * Create by TangJun on 2019/2/16
 */
public class ProblemTrainingFragment extends Fragment {
    private WrongFragmentBean hardFragmentBean;
    private TextView tvTitleNum;
    private RecyclerView recyclerView;
    private ImageView ivNoDate;
    private List<WrongItemListBean> list;
    private WrongItemAdapter adapter;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_problem, container, false);
        return view;    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        findViews(view);
    }

    private void findViews(View view) {
        tvTitleNum = view.findViewById(R.id.tv_problemNum);
        recyclerView = view.findViewById(R.id.rv_list);
        ivNoDate = view.findViewById(R.id.iv_nodate);
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ((PhoneMainActivity)getActivity()).getHardFragment();
    }
    public void setText(WrongFragmentBean bean){
        hardFragmentBean = bean;
        tvTitleNum.setText(bean.getWrongItemNum()+"");
    }

    public static List<PostCNtxlJsonBody.QuesIDBean> QuesID;
    public static List<PostCNtxlJsonBody.QuesIDBean> getQuesID() {
        return QuesID;
    }
    public static void setQuesID(List<PostCNtxlJsonBody.QuesIDBean> quesID) {
        QuesID = quesID;
    }
    public void setList(WrongFragmentBean bean){
        list = new ArrayList<>();
        for (int i = 0; i < bean.getModelWrongItemTypeListInfo().size(); i++ ){
            switch (bean.getModelWrongItemTypeListInfo().get(i).getWrongItemType()){
                case 2:
                    for (int j = 0;j < bean.getModelWrongItemTypeListInfo().get(i).getModelWrongItemListInfo().size();j++){
                        WrongItemListBean listBean = new WrongItemListBean();
                        listBean.setItemNum(bean.getModelWrongItemTypeListInfo().get(i).getModelWrongItemListInfo().get(j).getItemNum());
                        // listBean.setQuesIds(bean.getModelWrongItemTypeListInfo().get(i).getModelWrongItemListInfo().get(j).getQuesIds());
                        listBean.setQuesTypeName(bean.getModelWrongItemTypeListInfo().get(i).getModelWrongItemListInfo().get(j).getQuesTypeName());
                        listBean.setQuesTypeID(bean.getModelWrongItemTypeListInfo().get(i).getModelWrongItemListInfo().get(j).getQuesTypeID());
                        list.add(listBean);
                    }
                    break;
            }
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new WrongItemAdapter(list, new WrongItemAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                PhoneMainActivity.setCNtxlModel(2);

                QuesID = new ArrayList<>();
                int ItemNum = hardFragmentBean.getModelWrongItemTypeListInfo().get(1).getModelWrongItemListInfo().get(position).getItemNum();
                for(int k=0;k<ItemNum;k++){
                    PostCNtxlJsonBody.QuesIDBean QuesIDBean = new PostCNtxlJsonBody.QuesIDBean();
                    String QuesId = hardFragmentBean.getModelWrongItemTypeListInfo().get(1).
                            getModelWrongItemListInfo().get(position).getQuesIds().get(k).getQuesId();
                    String UpdateTime = hardFragmentBean.getModelWrongItemTypeListInfo().get(1).
                            getModelWrongItemListInfo().get(position).getQuesIds().get(k).getUpdateTime();
                    QuesIDBean.setQuesId(QuesId);
                    QuesIDBean.setUpdateTime(UpdateTime);
                    QuesID.add(k,QuesIDBean);
                    Log.e("==BODY=","========setQuesId======setQuesId======"+QuesId);
                }
                setQuesID(QuesID);


                Intent intent = new Intent();
                intent.setClass(getActivity(), CNtxlActivity.class);
                startActivity(intent);
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });
        recyclerView.setAdapter(adapter);
        if (list.isEmpty()){
            ivNoDate.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
        }else {
            ivNoDate.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);
            adapter.updateData(list);
        }
    }
}
